package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DayReportMapper;
import com.ruoyi.system.domain.DayReport;
import com.ruoyi.system.service.IDayReportService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-05
 */
@Service
public class DayReportServiceImpl implements IDayReportService {

    @Autowired
    private DayReportMapper dayReportMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public DayReport selectDayReportById(Long id) {
        return dayReportMapper.selectDayReportById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param dayReport 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DayReport> selectDayReportList(DayReport dayReport) {

        if(dayReport.getDate() == null){
            dayReport.setDate(new Date());
        }
        Date date = dayReport.getDate();
        List<DayReport> dayReports = dayReportMapper.selectDayReportList(dayReport);
        if(dayReports == null || dayReports.isEmpty()){
            List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType("room_number");
            sysDictData.forEach(dictData -> {
                DayReport report = new DayReport();
                report.setRoomNumber(dictData.getDictValue());
                packReport(report);
                report.setDate(date);
                insertDayReport(report);
                dayReports.add(report);
            });
        }
        computeReport(dayReports);
        return dayReports;
    }

    private void computeReport(List<DayReport> dayReports) {
        DayReport rep = new DayReport();
        rep.setRoomNumber("总计");
        packReport(rep);
        dayReports.forEach(report -> {
            rep.setTaiwanFeeDay(rep.getTaiwanFeeDay().add(report.getTaiwanFeeDay()));
            rep.setTaiwanFeeNight(rep.getTaiwanFeeNight().add(report.getTaiwanFeeNight()));
            rep.setSmoke(rep.getSmoke().add(report.getSmoke()));
            rep.setDrinks(rep.getDrinks().add(report.getDrinks()));
            rep.setRetail(rep.getRetail().add(report.getRetail()));
            rep.setRestaurant(rep.getRestaurant().add(report.getRestaurant()));
            rep.setDiscount(rep.getDiscount().add(report.getDiscount()));
            rep.setRefund(rep.getRefund().add(report.getRefund()));
            rep.setTotal(rep.getTotal().add(report.getTotal()));
            rep.setExpenditure(rep.getExpenditure().add(report.getExpenditure()));
        });
        dayReports.add(rep);
    }

    private void packReport(DayReport report) {
        report.setTaiwanFeeDay(BigDecimal.ZERO);
        report.setTaiwanFeeNight(BigDecimal.ZERO);
        report.setSmoke(BigDecimal.ZERO);
        report.setDrinks(BigDecimal.ZERO);
        report.setRetail(BigDecimal.ZERO);
        report.setRestaurant(BigDecimal.ZERO);
        report.setDiscount(BigDecimal.ZERO);
        report.setRefund(BigDecimal.ZERO);
        report.setTotal(BigDecimal.ZERO);
        report.setExpenditure(BigDecimal.ZERO);
    }

    @Override
    public List<DayReport> selectDayReportMonthList(DayReport dayReport) {
        if(dayReport.getDate() == null){
            dayReport.setDate(new Date());
        }
        List<DayReport> dayReports = dayReportMapper.selectDayReportMonthList(dayReport);
        if(dayReports != null && !dayReports.isEmpty()){
            computeReport(dayReports);
        }
        return dayReports;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param dayReport 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDayReport(DayReport dayReport) {
        return dayReportMapper.insertDayReport(dayReport);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param dayReport 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDayReport(DayReport dayReport) {
        return dayReportMapper.updateDayReport(dayReport);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDayReportByIds(Long[] ids) {
        return dayReportMapper.deleteDayReportByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDayReportById(Long id) {
        return dayReportMapper.deleteDayReportById(id);
    }
}